#include "regist.h"
#include "ui_regist.h"
#include "mainwindow.h"

#include <QMessageBox>
#include <QCursor>

Regist::Regist(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Regist)
{
    ui->setupUi(this);
    setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
}

Regist::~Regist()
{
    delete ui;
}

void Regist::on_regist_ok_clicked()
{
    //获取当前输入的用户名和密码
    QString input_name = this->ui->regist_name->text();
    QString input_password = this->ui->regist_password->text();

    if(input_name.length() < 1 || input_password < 1){
        //信息输入不全，提示错误
        QMessageBox::critical(this, "ERROR", "用户名和密码不能为空！", "确定");
    }
    else{
        if(input_name == "root"){
            if(input_password == "root"){
                QMessageBox::information(this, "SUCCESS", "登录成功！", "确定");
                //调用管理员窗口
                setCursor(Qt::BusyCursor);
                rm = new RootMenu();
                rm->setdbconn(dbconn);
                setCursor(Qt::ArrowCursor);
                this->hide();
                rm->show();
            }
            else{
                this->ui->regist_name->clear();
                this->ui->regist_password->clear();
                QMessageBox::critical(this, "FAILED", "用户名或密码错误！", "确定");
            }
        }
        else{
            QString sqlstr = QString("SELECT *FROM users WHERE Account = '%1'").arg(input_name);
            setCursor(Qt::BusyCursor);
            QSqlQuery find;
            find.exec(sqlstr);
            find.seek(0);
            QString account = find.value("Account").toString();
            QString pwd = find.value("Password").toString();
            if(!account.length() || pwd != input_password){
                this->ui->regist_name->clear();
                this->ui->regist_password->clear();
                setCursor(Qt::ArrowCursor);
                QMessageBox::critical(this, "ERROR", "用户名或密码错误！", "确定");
            }
            else{
                setCursor(Qt::ArrowCursor);
                QMessageBox::information(this, "SUCCESS", "登录成功！", "确定");
                //调用学生窗口
                setCursor(Qt::BusyCursor);
                stu = new student();
                stu->setdbconn(dbconn);
                stu->modify = false;
                stu->UID = account;
                stu->showinfo();
                setCursor(Qt::ArrowCursor);
                this->hide();
                stu->show();
            }
        }
    }
}

void Regist::on_regist_quit_clicked()
{
    this->close();
}

